/*
 * @Author: your name
 * @Date: 2022-01-23 16:48:45
 * @LastEditTime: 2022-02-21 16:56:52
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \vue-element-admin\src\utils\common.js
 */
/*
 * @公共函数
 */
const imgUrl = process.env.VUE_APP_IMG_URL === '/' ? '' : process.env.VUE_APP_IMG_URL
/**
 * @description:  中国时间转 yyyy-mm-dd 格式
 * @param  obj
 * @return {string}
 */
  function fmtDate(obj) {
	var date = new Date(obj);
	var y = 1900 + date.getYear();
	var m = "0" + (date.getMonth() + 1);
	var d = "0" + date.getDate();
	return y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length);
}   //中国时间转成 yyyy-mm-dd 时间


 function imageUrl(url) {
	return `${imgUrl}${url}`
}
function uploadUrl(filePath,fileName = ''){
	console.log(imageUrl('/crmApi/uploadGET')+'?filePath='+filePath+(fileName?'&fileName='+fileName:''))
	return imageUrl('/crmApi/uploadGET')+'?filePath='+filePath+(fileName?'&fileName='+fileName:'')
}




/**
 * @description: 获取当前时间
 * @return {string}
 */
 function sysTime() {
	var myDate = new Date();
	var year = myDate.getFullYear();
	var month = myDate.getMonth() + 1;
	var date = myDate.getDate();
	var h = myDate.getHours();
	var m = myDate.getMinutes();
	var s = myDate.getSeconds();
	var now =
		year +
		"-" +
		(month < 10 ? "0" : "") +
		month +
		"-" +
		(date < 10 ? "0" : "") +
		date +
		" " +
		(h < 10 ? "0" : "") +
		h +
		":" +
		(m < 10 ? "0" : "") +
		m +
		":" +
		(s < 10 ? "0" : "") +
		s;
	return now;
}

/**
 * @description: 
 * @param {*} data     将要排序的数组
 * @param {*} column   排序的字段
 * @param {*} sort     排序规则
 * @return {*}
 */
function bubblingSort(data , column , sort = 'ASC' ){
     
    
    if(sort == 'ASC'){
		for(let i=1;i<data.length;i++){
			for(let j=0;j<data.length-i;j++){
				if(data[j][column] > data[j+1][column] ){   //从小到大
				   let small =   data[j+1]
				   data[j+1]= data[j]
				   data[j] = small
				}
			}
		}
	}else if(sort == 'DESC'){
		for(let i=1;i<data.length;i++){
			for(let j=0;j<data.length-i;j++){
				if(data[j][column] < data[j+1][column] ){   //从大到小
				   let small =   data[j+1]
				   data[j+1] = data[j]
				   data[j]= small
				}
			}
		}
	}
	// console.log(data)

    return data

}


export default {
	install: function(Vue) {
		Vue.prototype.fmtDate = (obj) => fmtDate(obj);
		Vue.prototype.imageUrl = (url) => imageUrl(url);
		Vue.prototype.sysTime = (url) => sysTime(url);
		Vue.prototype.bubblingSort = (data , column , sort) => bubblingSort(data , column , sort);
		Vue.prototype.uploadUrl = (filePath,fileName) => uploadUrl(filePath,fileName);
	},
}
